/**
 * Copyright (c) 2008 - TheNittyGritty - Thomas Brekelmans
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package nl.thenittygritty.amc 
{

	/**
	 * ApplicationSettings is an example class which should be copied to the
	 * project package. Each project specific parameter (like gateway URLs)
	 * should be inserted in this class, see the example property for more info.
	 */
	public class ApplicationSettings 
	{
		public static const STATE_LIVE:String = "LIVE";		public static const STATE_STAGING:String = "STAGING";		public static const STATE_LOCAL:String = "LOCAL";
		
		private static var state:String;
		
		/**
		 * Private constructor for the ApplicationSettings, 
		 * this shouldn't be called.
		 */
		public function ApplicationSettings() 
		{
			throw new Error("ApplicationSettings shouldn't be instantiated.");
		}
		
		/**
		 * The value of the application state affects the returned values of 
		 * stored variables. This can only be ApplicationSettings.STATE_LIVE or 
		 * ApplicationSettings.STATE_STAGING or ApplicationSettings.STATE_LOCAL, 
		 * else an ArgumentError is thrown.
		 */
		public static function set STATE(state:String):void
		{
			if (
			state != ApplicationSettings.STATE_LIVE ||			state != ApplicationSettings.STATE_STAGING ||			state != ApplicationSettings.STATE_LOCAL)
			{
				throw new ArgumentError("Parameter value: " + state +
				" for parameter state is invalid.");	
			}
			
			ApplicationSettings.state = state;	
		}
		
		public static function get STATE():String
		{
			return state;
		}
		
		////////////////////////////////////////////////////////////////////////
		// example property	(statefull)												  //
		////////////////////////////////////////////////////////////////////////
		private static const EXAMPLE_LIVE:String = "exampleLive";		private static const EXAMPLE_STAGING:String = "exampleStaging";		private static const EXAMPLE_LOCAL:String = "exampleLocal";
		
		/**
		 * Returns the value of the EXAMPLE property based on the current state.
		 */
		public static function get EXAMPLE():String
		{
			return getProperty("EXAMPLE");
		}
		
		////////////////////////////////////////////////////////////////////////
		// module name property	(stateless)												  //
		////////////////////////////////////////////////////////////////////////
		private static const moduleName:String = "moduleName";
		
		/**
		 * Returns the value of the MODULE_NAME property (which is unaffected
		 * by the current state).
		 */
		public static function get MODULE_NAME():String
		{
			return moduleName;
		}
		
		/**
		 * Returns the statefull static property based on the given name.
		 * @param name	The name of the property that will be retrieved in the
		 * current state of the application (i.e. based on the value of state).
		 */		
		private static function getProperty(name:String):String
		{
			// Create a property name of the given name and the current state.
			var propertyName:String = name + "_" + ApplicationSettings.state;
			// Create a null String for the returning property data.
			var propertyData:String;
			
			// Try to get the static class property with the propertyName.
			try
			{
				propertyData = ApplicationSettings[propertyName];
			}
			catch (error:Error)
			{
				trace("ApplicationSettings::getProperty() No property was " +
				"found with the name: " + name + ". Current state: " + state);
			}
			
			// Return the property data, this is null when we found nothing.
			return propertyData;
		}
	}
}
